import {axiosClient} from "@/utils/axiosClient.js";
import {handleCommonParams} from "@/api/commonParams.js";

const requestUrl = '/template/instance'

const api = {

    /**
     * 新增流程运行时实例
     * @param instance
     * @returns {Promise<*|undefined>}
     */
    createInstance: (instance) =>
      axiosClient().post(`${requestUrl}/addInfo`, instance),

    /**
     * 根据ID删除流程运行时实例
     * @param id
     * @returns {Promise<*|undefined>}
     */
    deleteInstanceById: (id) =>
        axiosClient().remove(`${requestUrl}/removeInfo/${id}`),

    /**
     * 根据ID全量更新流程运行时实例
     * @param id
     * @param instance
     * @returns {Promise<*|undefined>}
     */
    updateInstanceFull: (id, instance) =>
        axiosClient().put(`${requestUrl}/modifyInfo/${id}`, instance),

    /**
     * 根据ID部分更新流程运行时实例
     * @param id
     * @param updatePartial
     * @returns {Promise<*|undefined>}
     */
    updateInstancePartial: (id, updatePartial) =>
        axiosClient().patch(`${requestUrl}/modifyPartialInfo/${id}`, updatePartial),

    /**
     * 根据ID查询流程运行时实例
     * @param id
     * @returns {Promise<*|undefined>}
     */
    getInstanceById: (id) =>
        axiosClient().get(`${requestUrl}/getInfo/${id}`),

    /**
     * 分页查询流程运行时实例
     * @param page
     * @param size
     * @param businessParam
     * @returns {Promise<axios.AxiosResponse<any>|undefined>}
     */
    getInstancePage: (page, size, businessParam) => {
        const params = new URLSearchParams();
        params.append('page', page)
        params.append('size', size)
        setBusinessParams(params, businessParam)
        return axiosClient().get(`${requestUrl}/getInfos?${params.toString()}`)
    },

    /**
     * 根据条件查询流程运行时实例
     * @param businessParam
     * @returns {Promise<axios.AxiosResponse<any>|undefined>}
     */
    getInstanceList: (businessParam) => {
        const params = new URLSearchParams();
        setBusinessParams(params, businessParam)
        return axiosClient().get(`${requestUrl}/getInfoList?${params.toString()}`)
    }

}

const setBusinessParams = (params, businessParam) => {
    if (businessParam) {
        if (businessParam.processId) {
            params.append('processId', businessParam.processId)
        }
        if (businessParam.processCode) {
            params.append('processCode', businessParam.processCode)
        }
        if (businessParam.subject) {
            params.append('subject', businessParam.subject)
        }
        if (businessParam.applicantId) {
            params.append('applicantId', businessParam.applicantId)
        }
        if (businessParam.applicantName) {
            params.append('applicantName', businessParam.applicantName)
        }
        if (businessParam.deptId) {
            params.append('deptId', businessParam.deptId)
        }
        if (businessParam.deptName) {
            params.append('deptName', businessParam.deptName)
        }
        if (businessParam.applyTime) {
            params.append('applyTime', businessParam.applyTime)
        }
        if (businessParam.currentNodeId) {
            params.append('currentNodeId', businessParam.currentNodeId)
        }
        if (businessParam.processStatus) {
            params.append('processStatus', businessParam.processStatus)
        }
        if (businessParam.cancelTime) {
            params.append('cancelTime', businessParam.cancelTime)
        }
        if (businessParam.completeTime) {
            params.append('completeTime', businessParam.completeTime)
        }
        handleCommonParams(params, businessParam);
    }
}

export default api
